import allure
from selenium.webdriver.common.by import By

from page.home_page import HomePage


class FreightTempPage(HomePage):
    _ADD = (By.XPATH, "//*[text()='添加运费模板']")
    _INPUX_BOX = (By.XPATH, '//*[@placeholder="请输入运费模板名称"]')
    _ADD_ADDRESS = (By.XPATH, "//*[text()=' 添加地区']")
    _ALL_ADDRESS = (By.XPATH, "//*[@class='transfer-title']//span")
    _移动 = (By.CSS_SELECTOR, ".el-button--primary.is-circle")
    _OK = (By.XPATH, "//*[text()='确定']")
    _SAVE = (By.XPATH, '//*[text()="保存"]')
    _SUCCESS = (By.XPATH, "//*[text()='创建成功']")

    @allure.step('添加运费模板')
    def add(self, name):
        with allure.step("点击添加运费模板"):
            self.find_and_click(self._ADD)
        with allure.step("输入模板名称"):
            self.find_and_send(name, self._INPUX_BOX)

        with allure.step("添加地址"):
            self.find_and_click(self._ADD_ADDRESS)
            self.set_time(1)
            self.find_and_click(self._ALL_ADDRESS)
            self.find_and_click(self._移动)
        with allure.step("点击添加"):
            self.find_and_click(self._OK)
        self.find_and_click(self._SAVE)
        self.set_time(1)
        self.wait_for_visible(self._SUCCESS)
        return self.get_text(self._SUCCESS)

    @allure.step('直接点击添加')
    def click_add(self):
        with allure.step("点击添加运费模板"):
            self.find_and_click(self._ADD)
        with allure.step("点击添加"):
            self.find_and_click(self._SAVE)
        self.set_time(1)
        result = self.get_text(self._FAIL)
        self._driver.back()
        return result

    _ALL = (By.XPATH, "//*[text()='全区域配送']")

    @allure.step('所有区域都配送')
    def all_address(self, name):
        with allure.step("点击添加运费模板"):
            self.find_and_click(self._ADD)
        with allure.step("输入模板名称"):
            self.find_and_send(name, self._INPUX_BOX)

        with allure.step("勾选全区域配送"):
            self.find_and_click(self._ALL)

        with allure.step("点击添加"):
            self.find_and_click(self._SAVE)
        self.set_time(1)
        return self.get_text(self._FAIL)

    @allure.step('输入名称，直接点击添加')
    def input_name_click_add(self, name):
        with allure.step("点击添加运费模板"):
            self.find_and_click(self._ADD)
        with allure.step("输入模板名称"):
            self.find_and_send(name, self._INPUX_BOX)
        with allure.step("点击添加"):
            self.find_and_click(self._SAVE)
        self.set_time(1)
        result = self.get_text(self._FAIL)
        self._driver.back()
        return result

    _FAIL = (By.CSS_SELECTOR, ".el-message__content")

    @allure.step('添加运费模板失败')
    def add_fail(self, name):
        with allure.step("点击添加运费模板"):
            self.find_and_click(self._ADD)
        with allure.step("输入模板名称"):
            self.find_and_send(name, self._INPUX_BOX)

        with allure.step("添加地址"):
            self.find_and_click(self._ADD_ADDRESS)
            self.set_time(1)
            self.find_and_click(self._ALL_ADDRESS)
            self.find_and_click(self._移动)
        with allure.step("点击添加"):
            self.find_and_click(self._OK)
        self.find_and_click(self._SAVE)
        self.set_time(1)
        result = self.get_text(self._FAIL)
        self._driver.back()
        return result

    _TEMP_INPUT = (By.XPATH, '//*[@placeholder="输入运费模板名称"]')
    _SEARCH = (By.XPATH, '//*[text()="查询"]')
    _RESULT = (By.CSS_SELECTOR, '.el-table__row')

    @allure.step('搜索运费模板')
    def search(self, name):
        with allure.step("输入模板名称"):
            self.find_and_send(name, self._TEMP_INPUT)
        with allure.step("点击查询"):
            self.find_and_click(self._SEARCH)
            self.set_time(1)
        return len(self.finds(self._RESULT))

    _NO_DATA = (By.XPATH, '//*[text()="暂无数据"]')

    @allure.step('搜索运费模板')
    def search_fail(self, name):
        with allure.step("输入模板名称"):
            self.find_and_send(name, self._TEMP_INPUT)
        with allure.step("点击查询"):
            self.find_and_click(self._SEARCH)
        self.set_time(1)
        return self.get_text(self._NO_DATA)

    _DELETE = (By.XPATH, "//*[text()='删除']")
    _QUERY = (By.XPATH, "//*[contains(text(),'确定')]")
    _DEL_SUC = (By.XPATH, "//*[text()='删除成功']")

    @allure.step('删除运费模板')
    def del_temp(self, name):
        with allure.step("输入模板名称"):
            self.find_and_send(name, self._TEMP_INPUT)
        with allure.step("点击查询"):
            self.find_and_click(self._SEARCH)
            self.set_time(1)
        with allure.step('删除模板'):
            self.find_and_click(self._DELETE)
            self.finds(self._QUERY)[1].click()
        self.set_time(1.5)
        return self.get_text(self._DEL_SUC)
